//
//  NewFeatureCell.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/23.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit

class NewFeatureCell: UICollectionViewCell {
    
   override init(frame: CGRect) {
    super.init(frame: frame)
    prepareUI()
    
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    var btnAction:(() -> Void)?
    
    func prepareUI(){
      //添加背景图片
        contentView.addSubview(backgrountImage)
        //添加按钮
        contentView.addSubview(goButton)
      backgrountImage.translatesAutoresizingMaskIntoConstraints = false
      goButton.translatesAutoresizingMaskIntoConstraints = false 
        
      //backgrountImage添加约束
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg":backgrountImage]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg":backgrountImage]))
        //goButton添加约束
        contentView.addConstraint(NSLayoutConstraint(item: goButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: goButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
        
    }
    //开始形变动画
    func startAnimationForgoBntton(){
        goButton.transform = CGAffineTransformMakeScale(0, 0)
        goButton.hidden = false 
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations:{() -> Void in
            
            self.goButton.transform = CGAffineTransformIdentity
        }
        , completion:{ (Bool) -> Void in
        
            print("完成形变动画")
        })
    }

    var imageIndex:Int? {
        
        didSet{
            //根据页码赋值新图片
            backgrountImage.image = UIImage(named: "new_feature_\(imageIndex!+1)")
            goButton.hidden = true 
        }
     
    }
    //懒加载
    private lazy var backgrountImage:UIImageView = UIImageView()
    private lazy var goButton:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "btnDidAction", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
               return btn
    }()
    
    
    func btnDidAction(){
       AppDelegate.outSwitchViewController(false)
        
    }
}
